import Vue from 'vue'
import VueRouter from 'vue-router'
// 引入仓库
import store from '../store'
//封装一个函数，用于判断是否含有当前url地址
function hasUrl(url) {
  return store.getters.getUserInfo.menus_url.some((item) => item == url);
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: ()=>import('../pages/login.vue')
  },
  {
    path: '/index',
    component: ()=>import('../pages/index.vue'),
    children:[
      {
        path:'/home',
        component:()=>import('../views/home.vue')
      },
      {
        path:'/menu',
        component:()=>import('../views/menu/index.vue'),
        meta:{
          title:'菜单管理'
        },
        
      },{
        path:'/roler',
        component:()=>import('../views/roler/index.vue'),
        meta:{
          title:'角色管理'
        },
        
      },{
        path:'/user',
        component:()=>import('../views/manger/index.vue'),
        meta:{
          title:'管理员管理'
        }
      },{
        // meiyong
        path:'/goodscate',
        component:()=>import('../views/goodscate/index.vue'),
        meta:{
          title:'商品分类'
        }
      },{

        path:'/goodsspecs',
        component:()=>import('../views/goodsspecs/index.vue'),
        meta:{
          title:'商品规格'
        }
      },{

        path:'/goods',
        component:()=>import('../views/goods/index.vue'),
        meta:{
          title:'商品管理'
        }
      },{

        path:'/banner',
        component:()=>import('../views/banner/index.vue'),
        meta:{
          title:'轮播图管理'
        }
      },
      {

        path:'/member',
        component:()=>import('../views/member/index.vue'),
        meta:{
          title:'会员管理'
        }
      },{

        path:'/seck',
        component:()=>import('../views/seck/index.vue'),
        meta:{
          title:'商品秒杀'
        }
      },
      {
        path:'*',
        redirect:'/menu'
      }
    ]
  },
  {
    path:'*',
    redirect:'/menu'
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 全局登录拦截
router.beforeEach((to, from, next) => {
  // 路径为登录就放行
  if (to.path=='/login') {
    next()
    return
  }
  // 如果存在登录状态，就放行
  
  if (store.state.userInfo) {
    next()
    return
  }
  // 以上都没有，强制转到登录
  next('/login')

})
export default router
